/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.servlet;

import br.com.deployment.daoImpl.AnexoDAOImpl;
import br.com.deployment.entities.Anexo;
import br.com.menulayout.util.Base64;
import java.io.IOException;
import java.io.OutputStream;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author roniere
 */
@WebServlet(name = "AnnexShowServlet", urlPatterns = "/show/*")
public class AnnexShowServlet extends HttpServlet {

    @EJB
    AnexoDAOImpl anexoService;
    
    private static final long serialVersionUID = 201011231103L;

    public AnnexShowServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.err.println("SERVLET");
        String text = request.getParameter("annex");
        Anexo anexo = new Anexo();
        anexo = anexoService.find(Long.parseLong(text));
        
        OutputStream out = response.getOutputStream();
        response.setContentType(anexo.getType());
        
        if(anexo.getContentByte() == null && anexo.getContent() != null){
            anexo.setContentByte(Base64.decode(anexo.getContent()));
        }
        response.getOutputStream().write(anexo.getContentByte());

        if (out != null) {
            out.close();
        }
    }
}
